﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;

namespace Mimoza.Apps.Console
{
    /// <summary>
    /// Application class is a core of application.
    /// </summary>
    public class Application
    {
        private Configuration m_config = null;
        private Mimoza.Common.IDBLayer m_DBLayer = null;
        private Mimoza.ObjectLayer.OL m_objectLayer = null;
        private Mimoza.PluginLayer.PluginManager m_pluginManager = null;

        private Mimoza.Apps.Console.ViewModel.ContainerTreeViewModel m_containersTreeViewModel = null;
        private Mimoza.Apps.Console.ViewModel.ToolBarViewModel m_toolBarViewModel = null;
        private Mimoza.Apps.Console.ViewModel.HostsViewModel m_hostsViewModel = null;
        private Mimoza.Apps.Console.ViewModel.PolicyViewModel m_policyViewModel = null;
        private Mimoza.Apps.Console.ViewModel.UpdateViewModel m_updateViewModel = null;

        private Mimoza.Apps.Console.ViewModel.QueryByPlatformViewModel m_queryByPlatformModel = null;
        private Mimoza.Apps.Console.ViewModel.QueryByTimeViewModel m_queryByTimeModel = null;
        
        bool m_bStopFlag = false;

        public Application(Configuration config)
        {
            m_config = config;

            m_toolBarViewModel = new Mimoza.Apps.Console.ViewModel.ToolBarViewModel();
            m_hostsViewModel = new Mimoza.Apps.Console.ViewModel.HostsViewModel();
            m_policyViewModel = new Mimoza.Apps.Console.ViewModel.PolicyViewModel();
            m_updateViewModel = new Mimoza.Apps.Console.ViewModel.UpdateViewModel();
            m_containersTreeViewModel = new Mimoza.Apps.Console.ViewModel.ContainerTreeViewModel();

            m_queryByPlatformModel = new Mimoza.Apps.Console.ViewModel.QueryByPlatformViewModel();
            m_queryByTimeModel = new Mimoza.Apps.Console.ViewModel.QueryByTimeViewModel();


            m_pluginManager = new Mimoza.PluginLayer.PluginManager();
        }

        public Configuration Configuration
        {
            get { return m_config; }
        }
        public Mimoza.ObjectLayer.OL ObjectLayer
        {
            get { return m_objectLayer; }
        }
        public Mimoza.PluginLayer.PluginManager PluginManager 
        {
            get { return m_pluginManager; }
        }

        public Mimoza.Apps.Console.ViewModel.ContainerTreeViewModel ContainersTreeViewModel
        {
            get
            {
                return m_containersTreeViewModel;
            }
        }
        public Mimoza.Apps.Console.ViewModel.ToolBarViewModel ToolBarViewModel
        {
            get
            {
                return m_toolBarViewModel;
            }
        }
        public Mimoza.Apps.Console.ViewModel.HostsViewModel HostsViewModel
        {
            get
            {
                return m_hostsViewModel;
            }
        }
        public Mimoza.Apps.Console.ViewModel.PolicyViewModel PolicyViewModel
        { 
            get
            {
                return m_policyViewModel;
            }
        }
        public Mimoza.Apps.Console.ViewModel.UpdateViewModel UpdateViewModel
        {
            get
            {
                return m_updateViewModel;
            }
        }
        public Mimoza.Apps.Console.ViewModel.QueryByPlatformViewModel QueryByPlatformViewModel
        {
            get
            {
                return m_queryByPlatformModel;
            }
        }
        public Mimoza.Apps.Console.ViewModel.QueryByTimeViewModel QueryByTimeViewModel
        {
            get
            {
                return m_queryByTimeModel;
            }
        }
        public void Init(System.Windows.Forms.Form mainForm)
        {
            Configuration.Init(mainForm);

            if (Configuration.UseLocalDatabase)
            {
                m_DBLayer = new Mimoza.DBLayer.SQLSC.SQLSC_DBLayer();
                m_DBLayer.Connect();
                m_objectLayer = new Mimoza.ObjectLayer.OL(m_DBLayer);
            }
            else
            {
                m_objectLayer = Configuration.ObjectLayer;
            }

            m_hostsViewModel.Init(this);
            m_policyViewModel.Init(this);
            m_updateViewModel.Init(this);
            m_toolBarViewModel.Init(this);
            m_containersTreeViewModel.Init(this);
            m_queryByTimeModel.Init(this);
            m_queryByPlatformModel.Init(this);

            m_pluginManager.Init();

            InitThread();
        }

        public void Stop()
        {   
            FinishThread();

            Configuration.Deinit();
        }

        void InitThread()
        {
            m_bStopFlag = false;

            m_thread = new Thread(new ThreadStart(DoWork));
            m_thread.Start();
        }

        public void FinishThread()
        {
            m_bStopFlag = true;

            if (m_thread != null)
            {
                m_thread.Join();
            }
        }


        public void DoWork()
        {
            while (!m_bStopFlag)
            {
                Transport.Transport.Dispatcher.DoProcess();
            }
        }

        Thread m_thread;

    }
}
